package com.hilaver.dzmis.service.impl;

import java.util.List;
import java.util.Set;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiMeasureSpecification;
import com.hilaver.dzmis.basicinfo.BiMeasureSpecificationItem;
import com.hilaver.dzmis.codetable.CtCollarType;
import com.hilaver.dzmis.codetable.CtMsiDescription;
import com.hilaver.dzmis.codetable.CtProductFamily;
import com.hilaver.dzmis.codetable.CtStature;
import com.hilaver.dzmis.dao.BiMeasureSpecificationDAO;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;
import com.hilaver.dzmis.util.StringUtils;

public class BiMeasureSpecificationServiceImpl extends AbstractBaseService {

	public final static String COLUMN_COLLAR = "ctCollarType";

	public final static String COLUMN_STATURE = "ctStature";
	
	public final static String COLUMN_REAL_PHOTO1_NAME = "realPhoto1Name";
	
	public final static String COLUMN_REAL_PHOTO2_NAME = "realPhoto2Name";
	
	public final static String COLUMN_ITEM_NUMBER = "number";
	
	public final static String COLUMN_MSITEM_MSID = "biMeasureSpecification.id";

	private CtServiceImpl ctService;
	
	private BiMeasureSpecificationDAO dao;

	public BiMeasureSpecificationServiceImpl() {
		this.dao = new BiMeasureSpecificationDAO();
		this.ctService = new CtServiceImpl();
	}

	public String edit(BiMeasureSpecification biMS) throws Exception {
		String operation = null;
		if (biMS.getId() != null) {
			BiMeasureSpecification old = (BiMeasureSpecification) this.dao.get(
					BiMeasureSpecification.class.getName(), biMS.getId());
			if(biMS.getRealPhoto1Name() == null){
				biMS.setRealPhoto1(old.getRealPhoto1());
				biMS.setRealPhoto1Name(old.getRealPhoto1Name());
			}
			if(biMS.getRealPhoto2Name() == null){
				biMS.setRealPhoto2(old.getRealPhoto2());
				biMS.setRealPhoto2Name(old.getRealPhoto2Name());
			}
			
			this.dao.getSession().evict(old);
			this.dao.saveOrUpdate(biMS);
			operation = "02";
		}else{
			if(biMS.getSimilarId() != null){
				BiMeasureSpecification similarOne = (BiMeasureSpecification) this.dao.get(
						BiMeasureSpecification.class.getName(), biMS.getSimilarId());
				if(biMS.getRealPhoto1Name() == null){
					biMS.setRealPhoto1(similarOne.getRealPhoto1());
					biMS.setRealPhoto1Name(similarOne.getRealPhoto1Name());
				}
				if(biMS.getRealPhoto2Name() == null){
					biMS.setRealPhoto2(similarOne.getRealPhoto2());
					biMS.setRealPhoto2Name(similarOne.getRealPhoto2Name());
				}
				this.dao.saveOrUpdate(biMS);
				
				Set<BiMeasureSpecificationItem> BiMSIs = similarOne.getBiMeasureSpecificationItems();
				for (BiMeasureSpecificationItem biMeasureSpecificationItem : BiMSIs) {
					BiMeasureSpecificationItem cloneOne = biMeasureSpecificationItem.cloneOne();
					cloneOne.setBiMeasureSpecification(biMS);
					this.dao.saveOrUpdate(cloneOne);
				}
				
			}else{
				this.dao.saveOrUpdate(biMS);
			}
			operation = "01";
		}
		super.editSysMbox(biMS, operation);
		return "{success: true}";
	}
	
	public String edit(BiMeasureSpecificationItem biMSitem) throws Exception{
		this.dao.saveOrUpdate(biMSitem);
		//this.dao.storeSysMbox(biMSitem);
		return "{success: true}";
	}

	@Override
	public String delete(int id) throws Exception {
		super.editSysMbox(BiMeasureSpecification.class.getName(), id);
		return super.delete(BiMeasureSpecification.class.getName(), id);
	}
	
	public String deleteItem(int id)throws Exception{
		return super.delete(BiMeasureSpecificationItem.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		BiMeasureSpecification biMS = (BiMeasureSpecification) this.dao.get(
				BiMeasureSpecification.class.getName(), id);
		
		writeFile(realFileDir + biMS.getRealPhoto1Name(), biMS.getRealPhoto1());
		writeFile(realFileDir + biMS.getRealPhoto2Name(), biMS.getRealPhoto2());
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
				
		if(biMS.getBiFranceCustomer() != null){
			xmlSB.append("<" + COLUMN_CUSTOMER_ID + ">");
			xmlSB.append(biMS.getBiFranceCustomer().getId());
			xmlSB.append("</" + COLUMN_CUSTOMER_ID + ">");
		}
		
		xmlSB.append("<" + COLUMN_REAL_PHOTO1_NAME + ">");
		if(biMS.getRealPhoto1Name() == null || "".equals(biMS.getRealPhoto1Name())){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			writeFile(realFileDir + biMS.getRealPhoto1Name(), biMS.getRealPhoto1());
			xmlSB.append(biMS.getRealPhoto1Name());
		}
		xmlSB.append("</" + COLUMN_REAL_PHOTO1_NAME + ">");
		
		xmlSB.append("<" + COLUMN_REAL_PHOTO2_NAME + ">");
		if(biMS.getRealPhoto2Name() == null || "".equals(biMS.getRealPhoto2Name())){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			writeFile(realFileDir + biMS.getRealPhoto2Name(), biMS.getRealPhoto2());
			xmlSB.append(biMS.getRealPhoto2Name());
		}
		xmlSB.append("</" + COLUMN_REAL_PHOTO2_NAME + ">");
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(biMS));
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public BiMeasureSpecification getObj(int id) throws Exception {
		return (BiMeasureSpecification)this.dao.get(BiMeasureSpecification.class.getName(), id);
	}

	public String getItem(int id) throws Exception{
		BiMeasureSpecificationItem biMSItem = (BiMeasureSpecificationItem) this.dao.get(
				BiMeasureSpecificationItem.class.getName(), id);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append("<msId>");
		xmlSB.append(biMSItem.getBiMeasureSpecification().getId());
		xmlSB.append("</msId>");
		xmlSB.append(SimpleObj2XML.toXMLFragment(biMSItem));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	
	public String getWithDescription(int id) throws Exception {
		BiMeasureSpecification biMS = (BiMeasureSpecification) this.dao.get(
				BiMeasureSpecification.class.getName(), id);
		writeFile(realFileDir + biMS.getRealPhoto1Name(), biMS.getRealPhoto1());
		writeFile(realFileDir + biMS.getRealPhoto2Name(), biMS.getRealPhoto2());
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(this.toXMLFragment(biMS));

		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List objList = this.dao.getAllPagination(BiMeasureSpecification.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (Object obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);

			xmlSB.append(this.toXMLFragment((BiMeasureSpecification) obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(BiMeasureSpecification.class.getName()));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	public String getAllPagination(int page, String sort, String order,
			String[] filter) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List objList = this.dao.getAllPagination(BiMeasureSpecification.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filter);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (Object obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);

			xmlSB.append(this.toXMLFragment((BiMeasureSpecification) obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(BiMeasureSpecification.class.getName(),
				filter));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	public String getItemAll(int msId) throws Exception {
		String [] filters = new String[1];
		filters[0] = COLUMN_MSITEM_MSID + "=" + msId;
		List<BiMeasureSpecificationItem> objList = this.dao.getAll(BiMeasureSpecificationItem.class.getName(), COLUMN_ITEM_NUMBER, "asc", filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		int offset = 0;
		for (BiMeasureSpecificationItem obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append("<ctMsiDescription>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtMsiDescription.class.getName(),obj.getCtMsiDescription(), locale));
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</ctMsiDescription>");
			
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(BiMeasureSpecificationItem.class.getName(),filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	private String toXMLFragment(BiMeasureSpecification biMS) throws Exception {
		StringBuffer xmlSB = new StringBuffer();

		xmlSB.append("<" + COLUMN_COLLAR + ">");
		xmlSB.append(this.ctService.getDescription(
				CtCollarType.class.getName(), biMS.getCtCollarType(), locale));
		xmlSB.append("</" + COLUMN_COLLAR + ">");

		xmlSB.append("<" + COLUMN_STATURE + ">");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtStature.class.getName(),
				biMS.getCtStature(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_STATURE + ">");
		
		xmlSB.append("<ctStatureCode>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(biMS.getCtStature());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctStatureCode>");
		
		xmlSB.append("<" + COLUMN_PRODUCT_FAMILY + ">");
		xmlSB.append(this.ctService.getDescription(CtProductFamily.class.getName(),
				biMS.getCtProductFamily(), locale));
		xmlSB.append("</" + COLUMN_PRODUCT_FAMILY + ">");
		
		if(biMS.getBiFranceCustomer() != null){
			xmlSB.append("<" + COLUMN_CUSTOMER_NAME + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biMS.getBiFranceCustomer().getName());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_CUSTOMER_NAME + ">");
			xmlSB.append("<" + COLUMN_CUSTOEMR_DEPARTMENT + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biMS.getBiFranceCustomer().getDepartment()==null?"":biMS.getBiFranceCustomer().getDepartment());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_CUSTOEMR_DEPARTMENT + ">");
		}
		
		xmlSB.append("<" + COLUMN_REAL_PHOTO1_NAME + ">");
		if(biMS.getRealPhoto1Name() == null || "".equals(biMS.getRealPhoto1Name())){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			writeFile(realFileDir + biMS.getRealPhoto1Name(), biMS.getRealPhoto1());
			xmlSB.append(biMS.getRealPhoto1Name());
		}
		xmlSB.append("</" + COLUMN_REAL_PHOTO1_NAME + ">");
		
		xmlSB.append("<" + COLUMN_REAL_PHOTO2_NAME + ">");
		if(biMS.getRealPhoto2Name() == null || "".equals(biMS.getRealPhoto2Name())){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			writeFile(realFileDir + biMS.getRealPhoto2Name(), biMS.getRealPhoto2());
			xmlSB.append(biMS.getRealPhoto2Name());
		}
		xmlSB.append("</" + COLUMN_REAL_PHOTO2_NAME + ">");
		
		xmlSB.append("<isAddItem>");
		if(biMS.getBiMeasureSpecificationItems().size() == 0){
			xmlSB.append("0");
		}else{
			xmlSB.append("1");
		}
		xmlSB.append("</isAddItem>");
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(biMS));
		
		return xmlSB.toString();
	}

	public String getMaxPatronageIndex(String ctStature, String ctCollarType) throws Exception {
		List patronageIndexList = this.dao.getMaxPatronageIndex(ctStature, ctCollarType);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(Constants.XML_CDATA[0]);
		if(patronageIndexList.size() == 0 || patronageIndexList.get(0) == null){
			xmlSB.append("0001");
		}else{
			Integer index = (Integer)patronageIndexList.get(0);
			xmlSB.append(StringUtils.to4Digital(++index));
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public static String[] getSizeName(String ctStature){
		if("02".equals(ctStature)){
			return new String[]{"B1", "B2", "B3", "B4", "B5"};
		}else if("03".equals(ctStature)){
			return new String[]{"T1", "T2", "T3", "T4", "T5"};
		}else if("04".equals(ctStature)){
			return new String[]{"S1", "M1", "L1", "S2", "M2", "L2"};
		}else if("07".equals(ctStature)){
			return new String[]{"0", "1", "2", "3", "4", "5", "6", "7"};
		}else if("01".equals(ctStature)){
			return new String[]{"", "S", "M", "L", "XL", "XXL", "", ""};
		}else{
			return new String[]{"XS", "S", "M", "L", "XL", "XXL", "XXXL", "4XL"};
		}
	}
}
